package br.usp.ime.dojo.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Identification extends Composite {

	private TextBox nameTB;
	private Button joinBTN;
	private Label nameLBL;
	private VerticalPanel identificationBox;
	private VerticalPanel panel;
	private IdentificationServiceAsync identificationSvc;
	private UserDTO loggedUser;
	private IdentificationListener identificationListener;
	private Label nameErrorLBL;

	public Identification() {

		nameLBL = new Label("Your name:");
		nameTB = new TextBox();
		joinBTN = new Button("Join!");
		joinBTN.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				identifyUser();
			}
		});

		nameTB.addKeyDownHandler(new KeyDownHandler() {
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					identifyUser();
				}
			}
		});
		panel = new VerticalPanel();
		identificationBox = new VerticalPanel();
		identificationBox.setStylePrimaryName("dojoBox");
		identificationBox.addStyleDependentName("identificationBox");
		identificationBox.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		identificationBox.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
		identificationBox.add(nameLBL);
		identificationBox.add(nameTB);
		identificationBox.add(joinBTN);
		panel.add(identificationBox);
		initWidget(panel);

		loggedUser = null;
	}

	public void setIdentificationListener(IdentificationListener listener) {
		identificationListener = listener;
	}

	private void identifyUser() {

		if (nameErrorLBL != null) {
			nameErrorLBL.removeFromParent();
			nameErrorLBL = null;
		}

		String name = nameTB.getText();
		if (name.isEmpty())
			setNameError("Name must not be empty.");
		else if (name.trim().isEmpty())
			setNameError("Name must not contain only whitespaces.");

		if (nameErrorLBL == null) {
			if (identificationSvc == null) {
				identificationSvc = GWT.create(IdentificationService.class);
			}
			AsyncCallback<UserDTO> callback = new AsyncCallback<UserDTO>() {
				public void onFailure(Throwable caught) {
					setNameError(caught.getMessage());
				}

				public void onSuccess(UserDTO result) {
					loggedUser = result;
					if (identificationListener != null) {
						identificationListener.onIdentificationSuccess(getLoggedUser());
					}
				}
			};
			// Make the call to the identification service.
			identificationSvc.identify(nameTB.getText(), callback);
		}
	}

	private void setNameError(String string) {
		nameErrorLBL = new Label(string);
		nameErrorLBL.setStylePrimaryName("dojoError");
		identificationBox.insert((nameErrorLBL), identificationBox.getWidgetIndex(nameTB));
	}

	public UserDTO getLoggedUser() {
		return loggedUser;
	}

}
